<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>View Attendance</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="./EldercareMasterPage/_finalstyle.css" rel="stylesheet" />
	<link href="./css/bootstrap.min.css" rel="stylesheet" media="screen">
	<link rel="stylesheet" type="text/css" href="./jquery/easyui.css">
	<link rel="stylesheet" type="text/css" href="./jquery/icon.css">   
</head>
<?php
	define('CONFIG_FILE',true);
	include './config.php';
	define('DB_FILE',true);

	session_start();
	if (empty($_SESSION["username"])||empty($_SESSION["userType"])) {
		Header("Location: ./Index.php");
		exit();
	}else{
		$userType = $_SESSION["userType"];
	}

	// set timeout period in seconds
	$inactive = 7200;
	// check to see if $_SESSION['timeout'] is set
	if (isset($_SESSION["timeout"])) {
		$session_life = time() - $_SESSION["timeout"];
		if ($session_life > $inactive) {
			session_destroy();
			Header("Location: Timeout.php");
		}
	}
	$_SESSION["timeout"] = time();
?>
<body>
    <div class="banner">
	</div>
		<div id="main">
			<div class="left-wrapper">
				<div class="logo">
                <img src="images/eldercare.gif" alt="logo" width="240px" />
				</div>
					<div class="menu-wrapper">
						<div id="cssmenu">
							<?php
								if ($userType == "Therapy Assistant") {
									include_once('./Sidebar/_SidebarTA.php');
								} else if ($userType == "Centre Manager") {
									include_once('./Sidebar/_SidebarCM.php');
								} else {
									include_once('./Sidebar/_SidebarSA.php');
								}
							?>
						</div>
					</div>		
                    <div class="clear"></div>
			</div>
			<div id="content">
				<div class="content2">
					<b style="float:right; margin: 0px 0 0 0">  
						<?php
							if (!empty($_SESSION["centreName"])) {
								$Username = $_SESSION["username"];
								$centreName = $_SESSION["centreName"];
								echo "Welcome, " . $Username . " ! " . $centreName . " Branch";
							} else {
								$Username = $_SESSION["username"];
								echo "Welcome, " . $Username;
							}
						?>
						<input type="submit" class="btn btn-danger" value="Logout" onclick="parent.location = 'logout.php'">
					</b>
					</p>
					<br>
					<div style="float:center">
					<br>
					<h3>Attendance Records </h3>
					<br>
					<table id="tt" class="easyui-datagrid" style="width:auto;height:430px"
							url="./Attendance/viewattendance.php"
							title="Searching" iconCls="icon-search" toolbar="#tb"
							rownumbers="true" pagination="true" rownumbers="true" fitColumns="true" singleSelect="true">
						<thead>
							<tr>
								<th field="NRIC" width="50" sortable="true" >NRIC</th>
								<th field="CustomerName" width="50" sortable="true">Client Name</th>
								<th field="CentreName" width="50" sortable="true" >Centre Name</th>
								<th field="SignInTimeStamp" width="50" align="right" sortable="true" >Sign In Time Stamp</th>
								<th field="SignOutTimeStamp" width="50" align="right" sortable="true" >Sign Out Time Stamp</th>
								<th field="Status" width="50" sortable="true">Status</th>
								<tbody style="display:none">
								<th field="AttendanceRecordID" width="50" >AttendanceRecordID</th></tbody>
							</tr>
						</thead>
					</table>
					<div id="tb" style="padding:3px">
						<?php
							if ($userType != "Therapy Assistant") { 
								echo "<a href='#' class='easyui-linkbutton' iconCls='icon-remove' plain='true' onclick='removeAtt()'>Remove</a>";
							}
						?>
						<span>NRIC:</span>
						<input id="NRIC" style="line-height:26px;border:1px solid #ccc">
						<span>Name: </span>
						<input id="CustomerName" style="line-height:26px;border:1px solid #ccc">
						<span>Date:</span> 
						<input id="SignInTimeStamp" class="easyui-datebox" />
						<?php
							require_once './Tool/DB/SqlDBManager.class.php';
							$sqlDBManager = new SqlDBManager();

							if($userType=="System Admin"){
								echo "<span>Centre:</span><select name='CentreLocation' id = 'CentreLocation' class='empty' style='width:150px'><option value='0'selected >Select a Centre</option>";

								$sql="SELECT CentreId, CentreName FROM centre";  
								$res=$sqlDBManager->queryRows($sql); 

								if(!empty($res)){
									for($i=0;$i<count($res);$i++){
										echo "<option value=\"".$res[$i]['CentreId']."\">".$res[$i]['CentreName']."</option>";			
									}
								}
								echo"
								</select>";
							}
							//close connection
    						$sqlDBManager->close_connect();
						?>
						<a href="#" class="easyui-linkbutton" iconCls="icon-search" onClick="doSearch()">Search</a>
						<a href="#" class="easyui-linkbutton" onClick="findAll()">All</a>
					</div>
					</div>
				</div>
			</div>
			<div class="clear"></div>
		</div>
	<div class="clear"></div>
	<div id="footer">&copy; 2013 MP22 IIT-IT Temasek Polytechnic</div>
</body>
<script type="text/javascript" src="./jquery/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="./jquery/jquery.easyui.min.js"></script>
<script type="text/javascript">
	$(function(){ 
        $("#SignInTimeStamp").next().find("input.validatebox-text").attr("readonly","readonly");  
    });
	$('#SignInTimeStamp').datebox({
	    formatter : function(date){
	        var y = date.getFullYear();
			var m = date.getMonth()+1;
			var d = date.getDate();
			return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
	    },
	    parser : function(s){
	        var t = Date.parse(s);
	        if (!isNaN(t)){
	            return new Date(t);
	        } else {
	            return null;
	        }
	    }
	});
	function doSearch(){
		//validate search input	
		var flagNRIC = 0;
		var flagCustomerName = 0;
		var flagSignInTimeStamp = 0;
		$.ajax({
			async : false,
			type : "POST",
			url : "./Attendance/validateSearch.php", //the page/method you wanna validate
			data: {
				NRIC: $('#NRIC').val(),
				CustomerName:$('#CustomerName').val(),
				SignInTimeStamp: $('.combo-text').val()
			},
			success : function(data) {
				var data = JSON.parse(data);
				flagNRIC=data.flagNRIC;
				flagCustomerName=data.flagCustomerName;
				flagSignInTimeStamp=data.flagSignInTimeStamp;
			}
		});
		if(flagNRIC==0){
			$.messager.show({
				title: 'Error',
                msg: 'Invalid Search Input. Search Input cannot contain any special characters except dots, dashes and single quotation marks.',
				showType:'fade',
				style:{
					right:'',
					bottom:''
				}
			});
		}else if(flagCustomerName==0){
			$.messager.show({
				title: 'Error',
                msg: 'Invalid Search Input. Search Input cannot contain any special characters except dots, dashes and single quotation marks.',
                showType:'fade',
				style:{
					right:'',
					bottom:''
				}
			});
		}else if(flagSignInTimeStamp==0){
			$.messager.show({
				title: 'Error',
                msg: 'Invalid Search Input. Search Input cannot contain any special characters except dots, dashes and single quotation marks.',
				showType:'fade',
				style:{
					right:'',
					bottom:''
				}
			});
		}else{
			$('#tt').datagrid('load',{
				NRIC: $('#NRIC').val(),
				CustomerName:$('#CustomerName').val(),
				SignInTimeStamp: $('.combo-text').val(),
				CentreLocation : $('#CentreLocation').val()		
			});
		}	
	}
	function findAll(){
		$('#tt').datagrid('load',{
		});
	}
	function removeAtt() {
		var row = $('#tt').datagrid('getSelected');
		if (row) {
			$.messager.confirm('Confirm Delete Attendance', 'Are you sure you want to remove this attendance record?', function(r) {
				if (r) {
					//validate AttendanceRecordID
					var check = 0;
					$.ajax({
						async : false,
						type : "POST",
						url : "./Attendance/validateRemove.php", //the page/method you wanna validate
						data: {
							AttendanceRecordID: row.AttendanceRecordID
						},
						success : function(data) {
							check=data;
						}
					});
					if(check==0){
						$.messager.show({
							title: 'Error',
							msg: 'Invalid Attendance Record ID to remove.',
							showType:'fade',
							style:{
								right:'',
								bottom:''
							}
						});
					}else{
						$.post('./Attendance/removeattendance.php', {AttendanceRecordID: row.AttendanceRecordID}, function(result) {
							if (result.success) {
								$('#tt').datagrid('reload');	// reload the user data
							}else{
								$.messager.show({// show error message
									title: 'Error',
									msg: result.msg,
									showType:'fade',
									style:{
										right:'',
										bottom:''
									}
								});
							}
						}, 'json');
					}
				}
			});
		}
	}
</script>
</html>